package com.aqie.easy.structure;

public class Trie {
    TrieNode root = new TrieNode('/');

    //用来插入一个string
    public void insert(String string){
        char[] c = string.toCharArray();
        TrieNode p = root;
        for(int i=0; i<c.length; i++){
            int index = c[i] - 'a';
            if(p.next[index] == null){
                TrieNode trienode = new TrieNode(c[i]);
                p.next[index] = trienode;
            }
            p = p.next[index];
        }
        p.isword = true;
    }

    //用来判断这个字符串是不是由其他节点依次加一个字符组成
    public boolean isBuild(String string){
        char[] c = string.toCharArray();
        TrieNode p = root;
        for(int i=0; i<c.length; i++){
            if(!p.next[c[i]-'a'].isword) return false;
            p = p.next[c[i]-'a'];
        }
        return true;
    }

}
